package org.example.judge_common.hander;

import lombok.extern.slf4j.Slf4j;
import org.example.judge_common.entity.ResponseResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义异常处理
 */
@ControllerAdvice
@Slf4j
public class BaseExceptionHandler {

    //统一 Exception 异常处理
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseResult error(HttpServletRequest request, HttpServletResponse response, Exception e) {

        if (e instanceof RetrunMsgException) {
            if(((RetrunMsgException) e).getResult()!=null)return ((RetrunMsgException) e).getResult();

            return ResponseResult.fail(e.getMessage());
        } else {
            log.error(e.getMessage());
            e.printStackTrace();
        }
        return ResponseResult.serverError();
    }
}
